package com.shop.servlet;

import com.shop.model.Course;
import com.shop.service.CourseService;
import com.shop.service.ICourseService;
import com.shop.service.IOrderService;
import com.shop.service.OrderService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/confirm_payment")
public class PaymentServlet extends HttpServlet {
    IOrderService iorderService = new OrderService();
    ICourseService icourseService = new CourseService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");


        String[] courseIdArray = req.getParameterValues("courseIds");
        for (String id : courseIdArray) {
            System.out.println("PaymentServlet:28:"+id);//调试
        }


        List<Course> courses = new ArrayList<>();
        for (String id : courseIdArray) {
            courses.add(icourseService.searchCourse1(id));
        }
        for (Course course : courses) {
            System.out.println("PaymentServlet:37:"+course.getCourseName());//调试
        }

//        System.out.println("PaymentServlet:38:"+req.getAttribute("courses"));//调试
//        List<Course> courses = (List<Course>) req.getAttribute("courses");

        String studentId = req.getParameter("studentId");
        System.out.println("PaymentServlet:39:"+req.getParameter("totalPrice").getClass()+" "+req.getParameter("totalPrice"));//调试
        Double totalPrice = Double.parseDouble(req.getParameter("totalPrice"));
        System.out.println("PaymentServlet:44:"+totalPrice);//调试


        List<String> courseIds = new ArrayList<>();//添加到课程List集合
        for(Course course:courses){
            courseIds.add(course.getCourseId());
        }
        for (String courseId : courseIds) {
            System.out.println("PaymentServlet:54:"+courseId);//调试
        }


        boolean paymoney = iorderService.paymoney(studentId, totalPrice);//判断余额是否够支付
        List<String> coursecapacity = iorderService.coursecapacity(courseIds);//判断所购买的课程容量是否充足
        List<String> include_course = iorderService.include_course(courseIds,studentId);//判断所购买的课程是否在数据库中存在


        if(coursecapacity.isEmpty() && paymoney && include_course.isEmpty()){
            //余额充足
            for (String courseId1 : courseIds) {
                iorderService.addOrder(studentId, courseId1, totalPrice, "已付款");//添加订单
            }
            iorderService.updateMoney(studentId,null);//更新余额
            iorderService.updateCourseCapacity(courseIds,-1);//更新课程容量
            System.out.println("PaymentServlet:70:成功");//调试
            req.setAttribute("courses", courses);
            req.setAttribute("studentId", studentId);
            req.getRequestDispatcher("result.jsp").forward(req, resp);
        }else if(!coursecapacity.isEmpty()){
            String joinedCourses = String.join(" ", coursecapacity);
            System.out.println("PaymentServlet:78:"+joinedCourses);//调试
            req.setAttribute("error", joinedCourses+" 这些课程名额已满,请重新选择！");
            req.setAttribute("studentId", studentId);
            req.getRequestDispatcher("Error.jsp").forward(req, resp);
        }else if (!paymoney){
            System.out.println("PaymentServlet:82:余额不足");//调试
            req.setAttribute("error", "账户余额不足，请充值！");
            req.setAttribute("studentId", studentId);
            req.getRequestDispatcher("Error.jsp").forward(req, resp);
        } else if (!include_course.isEmpty()) {
            String joinedincludeCourses = String.join(" ", include_course);
            req.setAttribute("error", joinedincludeCourses+" 这些课程名额已经报名过,请勿重复报名！");
            req.setAttribute("studentId", studentId);
            req.getRequestDispatcher("Error.jsp").forward(req, resp);
        }
    }
}
